﻿using System.Collections.Generic;

namespace Opo.Perspective.Core
{
    /// <summary>
    /// Represents a plugin for the PerspectivePipeline
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PerspectivePipeline<T> : IPerspectivePipeline<T>
    {
        private IList<IPerspectivePipelinePlugin<T>> _plugins;

        /// <summary>
        /// Gets or sets the plugins that are used for processing the object.
        /// </summary>
        /// <value>The plugins.</value>
        public IList<IPerspectivePipelinePlugin<T>> Plugins
        {
            get
            {
                if (_plugins == null)
                {
                    _plugins = new List<IPerspectivePipelinePlugin<T>>();
                }
                return _plugins;
            }
            set
            {
                _plugins = value;
            }
        }

        /// <summary>
        /// Processes the object.
        /// </summary>
        /// <param name="entity">The object to process.</param>
        /// <returns></returns>
        public T Process(T obj)
        {
            T objectToProcess = obj;
            foreach (IPerspectivePipelinePlugin<T> plugin in Plugins)
            {
                plugin.Process(ref objectToProcess);
            }
            return objectToProcess;
        }
    }
}
